<?php
require_once dirname(__FILE__) . '/libraries/ServiceBase.php';
/**
 * 开通盛大点券充值服务  
 * 开通划账服务  
 * 应用开通信息同步服务
 */
#@todo
class SDOServiceAddDeposit extends ServiceBase
{
    const ADD_DEPOSIT_WSDL = 'ADD_DEPOSIT_WSDL';
    const PRE_ADD_DEPOSIT_WSDL = 'PRE_ADD_DEPOSIT_WSDL';
    const FORBIDDEN_UPDATE_APPID_MAX = 500;
    const FORBIDDEN_APPID_RANGE_MIN = 10000;
    const FORBIDDEN_APPID_RANGE_MAX = 29999;
    /**
     * 应用开通信息同步服务
     * @param string $message
     * @param int $spId 商户ID
     * @param int $appId 应用ID
     * @param array $userList 授权用户列表
     * @param int $operation 0 同步所有 1仅同步应用状态 2仅同步授权用户 3 同步区组
     * @param int $appStatus 0关闭 1待运营 2上线
     * @param string $env 环境
     * 
     * @return boolean
     */
    public function AppSyncEx (&$message, $spId, $appId, $userList, $operation = 0, $appStatus = 1, $env='pre')
    {
        if (! self::validAppid($message, $appId, 0)) {
            return false;
        }
        $op = "AppSyncEx";
        $glue = "^|^";
        $appendixDefault = "";
        $UserListMaxLength = 600;
        $params = array(
            'SpId' => $spId, 
            'AppId' => $appId, 
            'UserList' => join($glue, $userList), 
            'Operation' => $operation, 
            'AppStatus' => $appStatus, 
            'AppendIx' => $appendixDefault
        );
        if (strlen($params['UserList']) > $UserListMaxLength) {
            $message = "User data too large.";
            return false;
        }
        $wsdl = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_ADD_DEPOSIT_WSDL) : sdk_fetch_config(SDK_ENV . self::ADD_DEPOSIT_WSDL);
        $option = array();
        $param = array(
            'req' => $params
        );
        //$param = $params;
        $result = self::soapRequest($op, $param, $wsdl, $option, $message);
        if ($result===false) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\texception: ".$message, __CLASS__);
        	return false;
        }
        //print_r($result);
        if ($result->AppSyncExResult->ResultCode === 0) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
            return true;
        } else {
            $message = $result->AppSyncExResult->ResultMsg;
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__);
        }
        return false;
    }
    /**
     * 开通盛大点券充值服务
     * @param string $message
     * @param int $spId
     * @param int $appId
     * @param date $beginDate 开通时间，目前只对绿卡有效 (Y-m-d H:i:s)
     * @param date $endDate 结束时间，目前只对绿卡有效 (Y-m-d H:i:s)
     * @param int $businessType 0 普通卡和绿卡 1  仅普通卡  2 绿卡
     * @param int $operation 0 关闭充值，1 开通充值
     * @param int $appStatus 0关闭 1待运营 2上线
     * @param string $env 环境
     * 
     * @return boolean
     */
    public function DepositConfigEx (&$message, $spId, $appId, $businessType = 1, $beginDate, $endDate, $operation = 1, $appStatus = 1, $env='pre')
    {
        if (! self::validAppid($message, $appId, $operation)) {
            return false;
        }
        
        $op = "DepositConfigEx";
        $appendixDefault = "";
        //if($businessType!=1){
        if (is_numeric($beginDate)) {
            $beginDate = date("Y-m-d H:i:s", $beginDate);
        } else {
            $beginDate = date("Y-m-d H:i:s", strtotime($beginDate));
        }
        if (is_numeric($endDate)) {
            $endDate = date("Y-m-d H:i:s", $endDate);
        } else {
            $endDate = date("Y-m-d H:i:s", strtotime($endDate));
        }
        //}
        $params = array(
            'SpId' => $spId, 
            'AppId' => $appId, 
            'BeginDate' => $beginDate, 
            'EndDate' => $endDate, 
            'BusinessType' => $businessType, 
            'Operation' => $operation, 
            'AppStatus' => $appStatus, 
            'AppendIx' => $appendixDefault
        );
        $wsdl = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_ADD_DEPOSIT_WSDL) : sdk_fetch_config(SDK_ENV . self::ADD_DEPOSIT_WSDL);
        $option = array();
        $param = array(
            'req' => $params
        );
        //$param = $params;
        $result = self::soapRequest($op, $param, $wsdl, $option, $message);
        if ($result===false) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\texception: ".$message, __CLASS__);
        	return false;
        }
        //print_r($result);
        if ($result->DepositConfigExResult->ResultCode === 0) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
            return true;
        } else {
            $message = $result->DepositConfigExResult->ResultMsg;
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__);
        }
        return false;
    }
    /**
     * 开通划账服务
     * @param $message 错误信息
     * @param $spId 商户ID
     * @param $appId 要开通划账的应用编码
     * @param $beginDate 开通时间 (Y-m-d H:i:s)
     * @param $endDate 结束时间(Y-m-d H:i:s)
     * @param $currencyName 游戏货币名称
     * @param $currencyUnit 游戏货币单位
     * @param $isLineRate 是否线性汇率，即1元能兑换多少整数个游戏币 1，线性  0 非线性
     * @param $maxPoint 划账支持的最大节点，单位元
     * @param $minPoint 划账支持的最小节点，单位元
     * @param array $pointList 划账支持的节点列表，格式：array(
     * array(amount1=>金额, amount2=>对应游戏币数量),
     * array(amount1=>金额, amount2=>对应游戏币数量)
     * ) 金额|对应游戏币数量#金额|对应游戏币数量 IsLineRate=1时，表示比率 ;长度小于等于600
     * @param int $businessType 0 普通卡和绿卡 1  仅普通卡  2 绿卡
     * @param $showAreaServer 是否显示区组 0不显示，1显示区，2显示服
     * @param int $operation 
     * @param int $appStatus 0关闭 1待运营 2上线
     * @param string $env 环境
     * 
     * @return boolean
     */
    public function TransferConfigEx (&$message, $spId, $appId, $beginDate, $endDate, $currencyName, $currencyUnit, $isLineRate, 
    $maxPoint, $minPoint, $pointList, $businessType = 1, $showAreaServer = 2, $operation = 1, $appStatus = 1, $env = 'pre')
    {
        if (! self::validAppid($message, $appId, $operation)) {
            return false;
        }
        $op = "TransferConfigEx";
        $appendixDefault = "";
        $glue1 = "|";
        $glue2 = "#";
        if (is_numeric($beginDate)) {
            $beginDate = date("Y-m-d H:i:s", $beginDate);
        } else {
            $beginDate = date("Y-m-d H:i:s", strtotime($beginDate));
        }
        if (is_numeric($endDate)) {
            $endDate = date("Y-m-d H:i:s", $endDate);
        } else {
            $endDate = date("Y-m-d H:i:s", strtotime($endDate));
        }
        $pointStr = "";
        if ($isLineRate == 0) {
            foreach ($pointList as $row) {
                $pointStr .= join($glue1, $row) . "#";
            }
            if ($pointStr) {
                $pointStr = substr($pointStr, 0, - 1);
            }
            if (isset($pointStr['600'])) {
            	$message = "节点列表长度超过600字节";
                return false;
            }
        } else {
            $pointStr = (int) $pointList;
        }
        $params = array(
            'SpId' => $spId, 
            'AppId' => $appId, 
            'BeginDate' => $beginDate, 
            'EndDate' => $endDate, 
            'CurrencyName' => $currencyName, 
            'CurrencyUnit' => $currencyUnit, 
            'IsLineRate' => $isLineRate, 
            'MaxPoint' => $maxPoint, 
            'MinPoint' => $minPoint, 
            'PointList' => $pointStr, 
            'BusinessType' => $businessType, 
            'ShowAreaServer' => $showAreaServer, 
            'Operation' => $operation, 
            'AppStatus' => $appStatus, 
            'AppendIx' => $appendixDefault
        );
        //print_r($params);
        $wsdl = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_ADD_DEPOSIT_WSDL) : sdk_fetch_config(SDK_ENV . self::ADD_DEPOSIT_WSDL);
        $option = array();
        $param = array(
            'req' => $params
        );
        //$param = $params;
        $result = self::soapRequest($op, $param, $wsdl, $option, $message);
        if ($result===false) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\texception: ".$message, __CLASS__);
        	return false;
        }
        //print_r($result);
        if ($result->TransferConfigExResult->ResultCode === 0) {
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__, true);
            return true;
        } else {
            $message = $result->TransferConfigExResult->ResultMsg;
            self::log("method: ".__FUNCTION__."\tparams: ".json_encode($param)."\tenv: {$env}\treturn: ".json_encode($result), __CLASS__);
        }
        return false;
    }
    /**
     * 
     * @param string $message 错误信息
     * @param int $appid
     * @param int 0 关闭，1开通
     */
    private static function validAppid (&$message, $appId, $op = 0)
    {
        if ($appId >= self::FORBIDDEN_APPID_RANGE_MIN && $appId <= self::FORBIDDEN_APPID_RANGE_MAX) {
            $message = "ID编号在10000-29999之间的，禁止开通支付服务。";
            return false;
        }
        if (! $op && $appId <= self::FORBIDDEN_UPDATE_APPID_MAX) {
            $message = "ID编号在1-500之间的，不被允许修改支付服务。";
            return false;
        }
        return true;
    }
	/**
     * 自检函数
     */
    public static function selfTest() 
    {
    	$arr = array(
        	sdk_fetch_config(SDK_ENV . self::ADD_DEPOSIT_WSDL),
        	sdk_fetch_config(SDK_ENV . self::PRE_ADD_DEPOSIT_WSDL)
    	);
    	return self::curlTest($arr);
    }
}